<?php
class product extends PHPUnit_Framework_TestCase{

    public function setUp()
    {
		$this->object = kernel::single('tbdata_api_tbapi');
		$this->cat_model = app::get('tbdata')->model('cats');
		$this->fetch_props_model = app::get('tbdata')->model('fetch_props');
		$this->product_model = app::get('tbdata')->model('product');
    }
	public function get_product($cid,$page_no=1){
		$params['cid'] = $cid;
		$params['page_size'] = 100;
		$params['page_no'] = $page_no;
		$params['method'] = 'taobao.products.search';
		$params['fields'] = 'product_id,outer_id,created,tsc,cid,cat_name,props,props_str,binds_str,sale_props_str,collect_num,name,binds,sale_props,price,desc,pic_url,modified,product_imgs,product_prop_imgs,status,level,pic_path,vertical_market,customer_props,property_alias';
		$products = $this->object->get_data($params);

		if($products['products_search_response']['products']['product']){
			$product_array = $products['products_search_response']['products']['product'];
			foreach($product_array as $product){
				$this->product_model->save($product);
				unset($product['id']);
			}

			if($page_no>1) return true;
			$total = $products['products_search_response']['total_results'];
			if($total>$params['page_size']){
				$count = ceil($total/$params['page_size']);
				for($i=2;$i<=$count;$i++){
					$this->get_product($cid,$i);
				}
			}

		}
	}
    public function testGetcat(){

		$rows = $this->fetch_props_model->getList('cid',array('status'=>'downloaded'),0,-1);
		$count = count($rows);
		$i = 1;
		foreach($rows as $row){
			echo "\n总数".$count.",已处理".$i++;
			$this->get_product($row['cid']);
		}
		

		print_r($cats);exit;

		

	}
}
